#include "mbappwig.h"
#include "ui_mbappwig.h"

#define TOPIC_CTL_DOWN      "/app/control/down"

MbappWig::MbappWig(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MbappWig)
{
    ui->setupUi(this);

    connect(GlobalVal::mqtt,SIGNAL(dataUpSignal(QByteArray)),this,SLOT(handleMqttMessage(QByteArray)));

    connect(ui->pushButtonSet,SIGNAL(clicked()),this,SLOT(setThresholdSlot()));

    connect(ui->pushButtonFan,SIGNAL(clicked()),this,SLOT(handleButtonGroup()));
    connect(ui->pushButtonDoor,SIGNAL(clicked()),this,SLOT(handleButtonGroup()));
    connect(ui->pushButtonInfrared,SIGNAL(clicked()),this,SLOT(handleButtonGroup()));
    connect(ui->pushButtonBeep,SIGNAL(clicked()),this,SLOT(handleButtonGroup()));

}

MbappWig::~MbappWig()
{
    delete ui;
}

void MbappWig::handleMqttMessage(const QByteArray &message)
{
    QJsonDocument doc = QJsonDocument::fromJson(message);
    QJsonObject obj = doc.object();

    int type = obj["type"].toInt();
    int result = obj["result"].toInt();

    QJsonArray dataArray = obj["data"].toArray();

    int fan_value,door_value,infrared_value,beep_value;
    double co2_value,h2o_value;

    for (int i = 0; i < dataArray.size(); i++)
    {
        QJsonObject dataObj = dataArray[i].toObject();
        int key = dataObj["key"].toInt();

        qDebug() << "key" <<key;
        QString val = dataObj["val"].toString();

        switch (key)
        {
        case 301:
            fan_value = val.toInt();
            //qDebug() << " fan_value" <<led_value;
            if(fan_value == 0)
            {
                ui->pushButtonFan->setText("Open");
            }
            else
            {
                ui->pushButtonFan->setText("Close");
            }
            break;
        case 302:
            door_value = val.toInt();
            if(door_value == 0)
            {
                ui->pushButtonDoor->setText("Open");
            }
            else
            {
                ui->pushButtonDoor->setText("Close");
            }
            break;
        case 303:
            infrared_value = val.toInt();
            if(infrared_value == 0)
            {
                ui->pushButtonInfrared->setText("Open");
            }
            else
            {
                ui->pushButtonInfrared->setText("Close");
            }
            break;
        case 304:
            beep_value = val.toInt();
            if(beep_value == 0)
            {
                ui->pushButtonBeep->setText("Open");
            }
            else
            {
                ui->pushButtonBeep->setText("Close");
            }
            break;
        case 305:
            co2_value = val.toDouble();
            ui->lcdNumberCo2->display(co2_value);
            break;
        case 306:
            h2o_value = val.toDouble();
            ui->lcdNumberDepth->display(h2o_value);
            break;
        default:
            break;
        }
    }
}

void MbappWig::setThresholdSlot()
{
    QJsonObject json;
    json["type"] = 5;
    QJsonObject data;
    data["Co2"] = ui->spinBoxCo2->value();
    data["Depth"] = ui->spinBoxDepth->value();
    json["data"] = data;
    QJsonDocument doc(json);
    QByteArray payload = doc.toJson();
    QMQTT::Message message;
    message.setTopic(TOPIC_CTL_DOWN); //设置主题
    message.setPayload(payload);//负载
    quint16 messageId = GlobalVal::mqtt->publish(message);
    if(messageId < 0)
    {
        QMessageBox::warning(this,"warn","设置失败");
    }
    else
    {
        QMessageBox::information(this,"info","设置成功");
    }
}

void MbappWig::handleButtonGroup()
{
    int fan_value = -1, door_value = -1, infrared_value = -1, beep_value = -1;

    if(sender() == ui->pushButtonFan)
    {
        if(ui->pushButtonFan->text() == "Open")
        {
            ui->pushButtonFan->setText("Close");
            fan_value = 1;
        }
        else if(ui->pushButtonFan->text() == "Close")
        {
            qDebug() << "close";
            ui->pushButtonFan->setText("Open");
            fan_value = 0;
        }
    }
    else if(sender() == ui->pushButtonDoor)
    {
        if(ui->pushButtonDoor->text() == "Open")
        {
            ui->pushButtonDoor->setText("Close");
            door_value = 1;
        }
        else if(ui->pushButtonDoor->text() == "Close")
        {
            ui->pushButtonDoor->setText("Open");
            door_value = 0;
        }
    }
    else if(sender() == ui->pushButtonInfrared)
    {
        if(ui->pushButtonInfrared->text() == "Open")
        {
            ui->pushButtonInfrared->setText("Close");
            infrared_value = 1;
        }
        else if(ui->pushButtonInfrared->text() == "Close")
        {
            ui->pushButtonInfrared->setText("Open");
            infrared_value = 0;
        }
    }
    else if(sender() == ui->pushButtonBeep)
    {
        if(ui->pushButtonBeep->text() == "Open")
        {
            ui->pushButtonBeep->setText("Close");
            beep_value = 1;
        }
        else if(ui->pushButtonBeep->text() == "Close")
        {
            ui->pushButtonBeep->setText("Open");
            beep_value = 0;
        }
    }

    // 创建 JSON 数据
    QJsonObject dataObject;
    if (fan_value != -1) {
        QJsonObject data_fan;
        data_fan["key"] = 301;
        data_fan["val"] = QString::number(fan_value);
        dataObject = data_fan;
    }
    if (door_value != -1) {
        QJsonObject data_door;
        data_door["key"] = 302;
        data_door["val"] = QString::number(door_value);
        dataObject = data_door;
    }
    if (infrared_value != -1) {
        QJsonObject data_infrared;
        data_infrared["key"] = 303;
        data_infrared["val"] = QString::number(infrared_value);
        dataObject = data_infrared;
    }
    if (beep_value != -1) {
        QJsonObject data_beep;
        data_beep["key"] = 304;
        data_beep["val"] = QString::number(beep_value);
        dataObject = data_beep;
    }

    // 生成根对象
    QJsonObject rootObject;
    rootObject["type"] = 2;
    rootObject["data"] = dataObject;

    // 调试输出 JSON 数据
    QJsonDocument doc(rootObject);
    qDebug().noquote() << doc.toJson(QJsonDocument::Indented);

    QByteArray payload = doc.toJson();
    QMQTT::Message message;
    //qDebug() << payload ;
    message.setTopic(TOPIC_CTL_DOWN); //设置主题
    message.setPayload(payload);//负载
    quint16 messageId = GlobalVal::mqtt->publish(message);
    // qDebug() << "messageId " << messageId ;
}
